package com.xianye.erp.service.order.impl;

import com.xianye.erp.dao.base.DaoSupport;
import com.xianye.erp.util.PageData;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

@Service("refreshOrderException")
@Scope("prototype")
public class RefreshOrderException {
	protected Logger log = Logger.getLogger(RefreshOrderException.class);
	private final static Executor executor = Executors.newCachedThreadPool();//启用多线程

	@Autowired
	private JdbcTemplate jdbc;
	@Resource(name = "daoSupport")
	private DaoSupport dao;
	@Autowired
	private ExpressOrderAPI expressOrderAPI;
	@Autowired
	private  OrderAPI orderAPI;
	@Autowired
	private OrderService orderService;
	
	public void refresh(){
		try {
			List<PageData> list = getException();
			System.out.println(list.size()+"大小");
			for(int i=0;i<list.size();i++){
				PageData pd = list.get(i);
				System.out.println(pd.toString());
				PageData store =(PageData)dao.findForObject("StoreMapper.findByStoreId",pd);
				System.out.println(store.toString());
				Long orderId = Long.parseLong(pd.getString("orderId"));
				Date gmtCreate = (Date) pd.get("gmtCreate");
				Date  start = new Date(gmtCreate.getTime()-1000);
				Date  end = new Date(gmtCreate.getTime()+1000);
				String create = gmtCreate.toLocaleString();
				JSONObject res = expressOrderAPI.getOrderList(store,1L,10L,start.toLocaleString(),end.toLocaleString(),null);
				System.out.println("异常数据"+res);
				JSONArray array = new JSONArray();
				if(res!=null && res.containsKey("order_list")){
					array = res.getJSONObject("order_list").getJSONArray("order_item_vo");
				}
				orderAPI.operateException(store,array);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public List<PageData> getException() throws Exception{
		return (List<PageData>) dao.findForList("OrderMapper.exceptionOrder", new PageData());
	}

	public void refreshOrderException(){
		try {
			List<PageData> list = (List<PageData>) dao.findForList("OrderMapper.findNoReceiptaddressOrder", new PageData());
			for(int i=0;i<list.size();i++){
				PageData order = list.get(i);
				PageData s = new PageData();
				s.put("storeId", order.get("STORE_ID"));
				PageData store = (PageData) dao.findForObject("StoreMapper.findByStoreId", s);
				JSONObject res = expressOrderAPI.getJSONOrderDetail(store, Long.parseLong(order.getString("orderId")));
				JSONObject receipt_address = res.getJSONObject("receipt_address");
				JSONObject buyer_info = res.getJSONObject("buyer_info");
				saveBuyer_info(buyer_info, order.getString("orderId"));
				saveReceiptaddress(receipt_address,order.getString("orderId"));
			}
			List<PageData> list1 = (List<PageData>) dao.findForList("OrderMapper.findNobuyerinfoOrder", new PageData());
			for(int i=0;i<list1.size();i++){
				PageData order = list1.get(i);
				PageData s = new PageData();
				s.put("storeId", order.get("STORE_ID"));
				PageData store = (PageData) dao.findForObject("StoreMapper.findByStoreId", s);
				JSONObject res = expressOrderAPI.getJSONOrderDetail(store, Long.parseLong(order.getString("orderId")));
				JSONObject receipt_address = res.getJSONObject("receipt_address");
				JSONObject buyer_info = res.getJSONObject("buyer_info");
				saveBuyer_info(buyer_info, order.getString("orderId"));
				saveReceiptaddress(receipt_address,order.getString("orderId"));
			}
		}catch (Exception e){
			e.printStackTrace();
		}
	}
	public void saveReceiptaddress(JSONObject oo,String orderId) throws Exception{
		PageData pd = new PageData();
		pd.put("orderId", orderId);
		pd.put("address", oo.get("address"));
		pd.put("address2", oo.get("address2"));
		pd.put("city", oo.get("city"));
		pd.put("contactPerson", oo.get("contact_person"));
		pd.put("country", oo.get("country"));
		pd.put("detailAddress", oo.get("detail_address"));
		pd.put("mobileNo", oo.get("mobile_no"));
		pd.put("phoneArea", oo.get("phone_area"));
		pd.put("phoneCountry", oo.get("phone_country"));
		pd.put("phoneNumber", oo.get("phone_number"));
		pd.put("province", oo.get("province"));
		pd.put("zip", oo.get("zip"));
		pd.put("CREATE_DATE", new Date());
		orderService.save_receiptaddress(pd);
	}
	public void saveBuyer_info(JSONObject oo,String orderId) throws Exception{
		PageData pd = new PageData();
		pd.put("orderId", orderId);
		pd.put("country", oo.get("country"));
		pd.put("firstName", oo.get("firstName"));
		pd.put("lastName", oo.get("lastName"));
		pd.put("loginId", oo.get("loginId"));
		//pd.put("Email", oo.get("Email"));
		pd.put("CREATE_DATE", new Date());
		orderService.save_buyerinfo(pd);
	}

}
